package com.tengju.user.infrastructure.repository.convert;

import com.tengju.user.domain.model.survey.*;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.SurveyConfigDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.SurveyContentDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.SurveyMetaDO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SurveyConvert {


    public static List<SurveyContentDO> convertContentToDOs(SurveyContent surveyContent) {
        List<SurveyContentDO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(surveyContent.getQuestionResults())) {
            return result;
        }
        long idCode = surveyContent.getIdCode().getValue();
        long surveyId = surveyContent.getSurveyId().getValue();
        Date now = new Date();
        surveyContent.getQuestionResults().forEach(v -> {
            SurveyContentDO surveyContentDO = new SurveyContentDO();
            surveyContentDO.setIdCode(idCode);
            surveyContentDO.setSurveyId(surveyId);
            surveyContentDO.setItemCode(v.getQuestionCode());
            surveyContentDO.setItemContent(v.getQuestionContent());
            surveyContentDO.setCreateTime(now);
            surveyContentDO.setUpdateTime(now);
            result.add(surveyContentDO);
        });
        return result;
    }

    public static SurveyMeta convertMetaDOToEntity(SurveyMetaDO surveyMetaDO) {
        if (surveyMetaDO == null) {
            return null;
        }
        return new SurveyMeta(
                new SurveyId(surveyMetaDO.getId()),
                surveyMetaDO.getTitle(),
                surveyMetaDO.getDesc(),
                surveyMetaDO.getItemNumLimit(),
                surveyMetaDO.getPassScore(),
                SurveyShuffleStatusEnum.parseByCode(surveyMetaDO.getShuffle()),
                SurveyTypeEnum.parseByCode(surveyMetaDO.getType())
        );
    }

    public static SurveyQuestionConfig convertQuestionConfigDOListToEntity(SurveyConfigDO surveyConfigDO) {
        if (surveyConfigDO == null) {
            return null;
        }
        return SurveyFactory.createQuestionConfig(
                surveyConfigDO.getItemCode(),
                surveyConfigDO.getItemTitle(),
                surveyConfigDO.getItemOrder(),
                surveyConfigDO.getItemType(),
                surveyConfigDO.getItemRequired(),
                surveyConfigDO.getItemParams(),
                surveyConfigDO.getMsgType()
        );
    }
}
